//
//  BarrackTower.m
//  TowerDefence
//
//  Created by k3 on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BarrackTower.h"
#import "SoldierFromBarrack.h"
#import "PlayScreen.h"

@implementation BarrackTower
@synthesize setRallyPointMode,rallyPoint,soldiers;

-(id)initBarrackTowerWith:(GameLayer *)_gameLayer{
    self=[super initTowerObjectWith:_gameLayer];
    rallyPoint=[CCSprite spriteWithFile:IMAGE_RALLY_FLAG_ICON];
    soldiers=[[NSMutableArray alloc] init];
    soldiersPositions=[[NSMutableArray alloc]init];
    maxRadiusFireTarget = 200.0f;
    setRallyPointMode=NO;
    countdown=0;
    rallyPoint.visible=NO;
    buyPrice = BARRACK_TOWER_CONSTRUCTION_PRICE;
    [self loadBarrackTower];
    return self;
}

-(void)loadBarrackTower{
    temp=[[CCSprite alloc]initWithSpriteFrameName:@"Barrack LV1/Barrack_01.png"];
    menuItemSpriteBarrackTower = [CCMenuItemSprite itemFromNormalSprite:temp selectedSprite:nil target:self selector:@selector(showOption)];
    menuBarrackTower = [CCMenu menuWithItems:menuItemSpriteBarrackTower, nil];
    menuBarrackTower.position = ccp(0, 0);
    [spriteBody addChild:menuBarrackTower];
    [((PlayScreen*)handler).mapBackgroundSprite addChild:rallyPoint];
}

-(void)update:(ccTime)_timeUpdate {
//    NSLog(@"base x=%f y=%f",spriteBody.position.x,spriteBody.position.y);
    //NSLog(@"Count of barrack = %d",soldiers.count);
    if (soldiers.count<3) {
        countdown++;
        if (countdown>200) {
            [self createSoldier];
            countdown=0;
        }
    }
}

-(void)createSoldier{
    //    NSLog(@"Tower position x=%f,y=%f",spriteBody.position.x,spriteBody.position.y);
    SoldierFromBarrack *soldier=[[SoldierFromBarrack alloc]initBarrackSoldierWith:handler withBarrack:self];
    [handler addGameObject:soldier];
    [((PlayScreen*)handler).mapBackgroundSprite addChild:soldier.spriteBody]; 
    soldier.spriteBody.position=ccp(spriteBody.position.x,spriteBody.position.y-20);
    //fixed
    for (int i=0;i<soldiers.count;i++){
        SoldierFromBarrack *tempSoldier=[soldiers objectAtIndex:i];
        tempSoldier.standingPosition=[[soldiersPositions objectAtIndex:i] CGPointValue];
    }
}

-(void)showOption{
    [super showOption];
    if(!rallyPoint.visible) {
        rallyPoint.visible=YES;
    }
    else{
        rallyPoint.visible=NO;
    }
}

-(void)sellTowerObjectCallback{
    [super sellTowerObjectCallback];
    for(int i=0;i<soldiers.count;i++){
        SoldierFromBarrack *soldier=[soldiers objectAtIndex:i];
        [handler removeGameObject:soldier];
        [soldier.spriteBody removeFromParentAndCleanup:YES]; 
    }
    [rallyPoint removeFromParentAndCleanup:YES];
}

-(void)newRallyPoint:(CGPoint)_newRallyPoint{
    [soldiersPositions removeAllObjects];
    rallyPoint.position=_newRallyPoint;
    for (int i=0;i<3;i++){
        float radian=[self degreesToRadians:(90+i*120)];
        CGPoint soldierPosition=ccp(rallyPoint.position.x+30*cos(radian),rallyPoint.position.y+30*sin(radian));
        [soldiersPositions addObject:[NSValue valueWithCGPoint:soldierPosition]];
        if (i<soldiers.count){
            SoldierFromBarrack *soldier=[soldiers objectAtIndex:i];
            soldier.standingPosition=[[soldiersPositions objectAtIndex:i] CGPointValue];
        }
//        [soldiersPositions addObject:[NSValue valueWithCGPoint:soldierPosition]];
//        CCSprite *temp=[CCSprite spriteWithFile:@"Images/blank.png"];
//        temp.position=soldierPosition;
//        temp.color=ccc3(255, 0, 0);
//        temp.textureRect = CGRectMake(0,0,8,8);
//        [((PlayScreen*)handler).mapBackgroundSprite addChild:temp];
    }
}

-(void)initOptionMenu{
    [super initOptionMenu];
    CCSprite *rallyMenuSprite = [CCSprite spriteWithFile:IMAGE_RALLY_TOWER_ICON];
    
    CCSprite *rallyTowerLabelSprite = [CCSprite spriteWithFile:IMAGE_MINI_INFO_TABLE];
    rallyTowerLabelSprite.position = ccp(rallyTowerLabelSprite.contentSize.width/2, 0);
    CCLabelTTF *rallyTowerLabel = [CCLabelTTF labelWithString:@"Rally" fontName:FONT_COMIC_BOOK fontSize:10];
    rallyTowerLabel.position = ccp(rallyTowerLabelSprite.contentSize.width/2, rallyTowerLabelSprite.contentSize.height/2);
    rallyTowerLabel.color = ccc3(255, 254, 64);
    [rallyTowerLabelSprite addChild:rallyTowerLabel];
    [rallyMenuSprite addChild:rallyTowerLabelSprite];
    
    CCMenuItemSprite *rallyTowerMenuItemSprite = [CCMenuItemSprite itemFromNormalSprite:rallyMenuSprite selectedSprite:nil target:self selector:@selector(setToRallyMode:)];
    CCMenu *rallyTowerMenu = [CCMenu menuWithItems:rallyTowerMenuItemSprite, nil];
    rallyTowerMenu.position = ccp(-50, 50);
    
    [optionMenu addChild:rallyTowerMenu];
}

-(void)setToRallyMode:(id)sender {
    PlayScreen* playScreen=(PlayScreen*)handler;
    playScreen.temp=self;
    playScreen.setRallyPointMode=YES;
    [optionMenu runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.2f scale:0.0f], nil]];
}

-(void)createStartRallyPoint{
    PlayScreen *playScreen=(PlayScreen*)handler;
    NSMutableArray *wayPoint;
    float distance=-1.0f;
    for (int i=0;i<playScreen.gameMapData.ways.count;i++){
//        NSLog(@"Count %d",[[playScreen.gameMapData.ways objectAtIndex:i] count]);
        for (int j=0;j+1<[[playScreen.gameMapData.ways objectAtIndex:i] count];j++){
//            NSLog(@"Loop %d time",j);
            wayPoint=[playScreen.gameMapData.ways objectAtIndex:i];
            CGPoint pointStart=[[wayPoint objectAtIndex:j]CGPointValue];
            CGPoint pointEnd=[[wayPoint objectAtIndex:j+1]CGPointValue];
            //fixed
            float distance1=ccpDistance(spriteBody.position, pointStart);
            float distance2=ccpDistance(spriteBody.position, pointEnd);
            if (distance==-1.0f || distance>distance1+distance2){
                distance=distance1+distance2;
                rallyPoint.position=ccpMidpoint(pointStart, pointEnd);
            }
        }
    }
}

-(void)create3SoldierAtStart{
    while(soldiers.count<3){
        [self createSoldier];
    }
}

-(void)dealloc {    
    [temp stopAllActions];
    [temp removeAllChildrenWithCleanup:YES];
    [temp removeFromParentAndCleanup:YES];
    [temp release];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self removeFromParentAndCleanup:YES];
    
    [super dealloc];
}

@end
